package com.apexsoft.monitor.front.console.log.service;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.DateUtil;
import com.apexsoft.front.common.utils.JsonResult;
import com.apexsoft.monitor.front.console.log.dao.LogDao;
import com.apexsoft.monitor.front.console.log.om.Log;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 
 * @author lsj
 *
 */
@Service("logService")
public class LogService {
	
	@Autowired
	@Qualifier("logDao")
	private LogDao logDao = null;
	
	public void add(Log log){
		logDao.insert(log);
	}
	
	public JSONObject queryLogByPage (String key) {
		JSONObject result = new JSONObject();
		List<Log> list = logDao.queryLogByPage(key);
		result.put(Result.TOTAL, list.size());
		result.put("root", list);
		return result;
	}

	/**
	 * 获取最大的ID值
	 * @param app_code
	 * @param node_code
	 * @param level
     * @return
     */
	public String queryLogByLastId(String app_code,String node_code,String level){
		HashMap<String ,String> map = new HashMap<>();
		map.put("app_code",app_code);
		map.put("node_code",node_code);
        if(StringUtils.isNotEmpty(level)){
            map.put("level",level);
        }
		String id = logDao.queryLogByLastId(map);
		return id;
	}

	/**
	 * 获取某后ID之后的日志数据
	 * @param app_code
	 * @param node_code
	 * @param level
	 * @param id
     * @return
     */
	public JSONObject queryLogAfterId(String app_code,String node_code,String level,String id,String task_code){
		HashMap<String ,Object> map = new HashMap<>();
		map.put("app_code",app_code);
		map.put("node_code",node_code);
        map.put("task_code",task_code);
		map.put("id",id);
        if(StringUtils.isNotEmpty(level)){
            List<String> list = new ArrayList<String>();
			if(level.equals("debug")){
                list.add("debug");
                list.add("info");
                list.add("warn");
                list.add("error");
            }
            if(level.equals("info")){
                list.add("info");
                list.add("warn");
                list.add("error");
            }
            if(level.equals("warn")){
                list.add("warn");
                list.add("error");
            }
            if(level.equals("error")){
                list.add("error");
            }
            map.put("level",list);
        }
		List<Log> list = logDao.queryLogAfterId(map);
        JSONObject result = new JSONObject();
        result.put("root", list);
		return result;
	}

	public JSONObject queryLogTask(String node_code){
		HashMap<String ,Object> map = new HashMap<>();
		map.put("node_code",node_code);
		List<HashMap> list = logDao.queryTaskForNodeAttrAndTemplate(map);
		JSONObject result = new JSONObject();
		result.put("root", list);
		return result;
	}

	public Log queryDetail (long id) {
		return logDao.selectById(id);
	}

	public JSONObject listByPageForEasyui (int page, int pageCount, String appCode, String nodeCode, String level, String startTime, String endTime,String content) {
		try{
			Log user = new Log();
			Page<Log> pageDdApp = new Page<Log>(page, pageCount);
			EntityWrapper<Log> entityWrapper = new EntityWrapper<Log>(user);
			entityWrapper.orderBy("id", false);
			if(StringUtils.isNotEmpty(appCode)){
				entityWrapper.and("app_code={0}",appCode);
			}
			if(StringUtils.isNotEmpty(nodeCode)){
				entityWrapper.and("node_code={0}",nodeCode);
			}
			if(StringUtils.isNotEmpty(level)){
				if (level.equals("debug")){
					entityWrapper.in("level",new String[]{"debug","info","warn","error"});
				}else if (level.equals("info")){
					entityWrapper.in("level",new String[]{"info","warn","error"});
				}else if (level.equals("warn")){
					entityWrapper.in("level",new String[]{"warn","error"});
				}else if (level.equals("error")){
					entityWrapper.in("level",new String[]{"error"});
				}
			}
			if (StringUtils.isBlank(startTime)) startTime = DateUtil.getNowDate();
			if (StringUtils.isBlank(endTime)) startTime = DateUtil.getNowDate();
			if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)){
				entityWrapper.between("date",startTime, endTime);
			}
			if(StringUtils.isNotBlank(content)){
				entityWrapper.like("content",content);
			}
			List<Log> ddApps = logDao.selectPage(pageDdApp, entityWrapper);
			int count = logDao.selectCountByEw(entityWrapper);
			JSONObject obj = new JSONObject();
			obj.put(Result.TOTAL, count);
			obj.put("rows", ddApps);
			return new JsonResult(true,"获取日志列表成功！").putObject(obj).getResult();
		}catch(Exception e){
			return new JsonResult().getExceptionResult(e);
		}
	}



	public JSONObject queryLogForLimit(int id,int page,int size,String type){
		JSONObject result = new JSONObject();
		int row = logDao.queryDataRow(id);
		//0 向上  1 向下
		List<Log> logList = null;
		if("0".equals(type)){
			row = 0 + ((page-1)*size);
			logList = logDao.queryLogsForLimit(row,size,id);
		}else if("1".equals(type)){
			row = row + ((page-1)*size);
			logList = logDao.queryLogsForLimit(row,size,null);
		}

		result =  Result.commonListResult(logList);

		return result;
	}

}
